package study.saas.order.service;

import org.springframework.stereotype.Service;
import study.saas.common.entity.YPageable;
import study.saas.common.repo.Repo;
import study.saas.common.util.CopyClassUtil;
import study.saas.common.util.PageUtils;
import study.saas.common.util.StringUtil;
import study.saas.common.viewmodel.ApiResultV2;
import study.saas.common.viewmodel.PageModel;
import study.saas.order.entity.Commodity;
import study.saas.order.repo.CommodityRepo;
import study.saas.order.service.model.CommodityFiterDTO;
import study.saas.order.service.model.CommodityModifyDTO;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class CommodityService {

    @Resource
    private CommodityRepo commodityRepo;


    /**
     * 新增
     * @param commodity
     * @return
     */
    public ApiResultV2 create(CommodityModifyDTO commodity) {
        Commodity firstByCommodityName = commodityRepo.findFirstByCommodityName(commodity.getCommodityName());
        if (firstByCommodityName!=null && !firstByCommodityName.isDelete()){
            return ApiResultV2.error("名称重复");
        }
        Commodity newCommodity = new Commodity();
        if (firstByCommodityName==null){
            CopyClassUtil.copyClass(commodity,newCommodity);
        }else {
            newCommodity = firstByCommodityName;
            newCommodity.setIsDeleted(0);
        }

        commodityRepo.save(newCommodity);
        return ApiResultV2.ok();
    }


    /**
     * 修改
     * @param commodity
     * @return
     */
    public ApiResultV2<Commodity> modify(CommodityModifyDTO commodity) {
        Commodity firstByCommodityName = commodityRepo.findFirstByCommodityName(commodity.getCommodityName());
        if (firstByCommodityName == null|| firstByCommodityName.isDelete()) {
            return ApiResultV2.error("商品不存在");
        }
        CopyClassUtil.copyClass(commodity, firstByCommodityName);
        commodityRepo.save(firstByCommodityName);
        return ApiResultV2.ok();
    }


    /**
     * 查询
     * @param commodityName
     * @return
     */
    public ApiResultV2<Commodity> findCommodity(String commodityName) {
        Commodity firstByCommodityName = commodityRepo.findFirstByCommodityName(commodityName);
        if (firstByCommodityName==null&&firstByCommodityName.isDelete()){
            firstByCommodityName = null;
        }
        return ApiResultV2.ok(firstByCommodityName);
    }


    public ApiResultV2<PageModel<Commodity>> list(CommodityFiterDTO filter) {
        YPageable pageable = PageUtils.Ypageable(filter.getPage(), filter.getPageSize());
        StringBuilder sql = new StringBuilder();
        Map<String, Object> param = new HashMap<>();
        sql.append(" select c from Commodity c where c.isDeleted = 0 ");
        if (!StringUtil.isEmpty(filter.getCommodityName())) {
            sql.append(" and c.commodityName = :getCommodityName");
            param.put("getCommodityName", filter.getCommodityName());
        }
        sql.append(" order by c.createTime desc,uuid desc ");
        pageable.setCountSql(Repo.toCountJPql(sql.toString()));
        PageModel<Commodity> pageModel = commodityRepo.queryPage(sql.toString(), param, Commodity.class, pageable);
        return ApiResultV2.ok(pageModel);
    }

    /**
     * 删除
     * @param commodityName
     * @return
     */
    public ApiResultV2 deleteCommodity(String commodityName) {
        Commodity firstByCommodityName = commodityRepo.findFirstByCommodityName(commodityName);
        if (firstByCommodityName==null&&firstByCommodityName.isDelete()){
            return ApiResultV2.ok();
        }
        firstByCommodityName.setIsDeleted(1);
        commodityRepo.save(firstByCommodityName);
        return ApiResultV2.ok();
    }

    public ApiResultV2 setAmount(Boolean flag, String commodityName, Integer num) {
        Commodity commodity = commodityRepo.findFirstByCommodityName(commodityName);
        if (commodity==null||commodity.isDelete()){
            return ApiResultV2.error("商品不存在！");
        }

        if (flag){
            commodity.setAmount(commodity.getAmount()+num);
        }else {
            commodity.setAmount(commodity.getAmount()-num);
        }
        commodityRepo.save(commodity);
        return ApiResultV2.ok();
    }
}
